package com.mimc.controller.admin;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mimc.common.constant.EventConstants;
import com.mimc.core.model.SysTranslate;
import com.mimc.core.service.SysTranslateService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.framework.domain.KV;
import io.jboot.Jboot;
import io.jboot.db.model.Columns;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/admin/sys/translate")
public class SysTranslateController extends JbootController {
    @Inject
    private SysTranslateService sysTranslateService;
    public AjaxResult lists(){
        Integer page=getInt("page",1);
        Integer pageSize=getInt("pageSize",15);
        String translateKey=getPara("translateKey");
        Columns columns=Columns.create();
        if(StrUtil.isNotEmpty(translateKey)){
            columns.like("translate_key","%"+translateKey+"%");
        }
        Page<SysTranslate> list=sysTranslateService.paginateByColumns(page,pageSize, columns,"create_time desc");
        return AjaxResult.success("",list);
    }
    public AjaxResult add(){
        String translateKey=getPara("translateKey");
        String remark=getPara("remark");
        String names=getPara("names");
        List<KV> kvlist= JSON.parseArray(names,KV.class);
        Record translate = new Record();

        translate.set("translate_key",translateKey);
        translate.set("remark",remark);
        translate.set("create_time",new Date());
        translate.set("update_time",new Date());
        for (int i=0;i<kvlist.size();i++){
            KV kv=kvlist.get(i);
            translate.set(kv.getKey(),kv.getValue());
        }
        Db.save("sys_translate", translate);
        Jboot.sendEvent(EventConstants.LOADTRANSLATEEVENT,"");
        return AjaxResult.success();
    }

    public AjaxResult update(){
        Integer translateId=getInt("translateId");
        String translateKey=getPara("translateKey");
        String remark=getPara("remark");
        String names=getPara("names");
        List<KV> kvlist= JSON.parseArray(names,KV.class);
        Record translate=Db.findById("sys_translate","translate_id",translateId);
        translate.set("translate_key",translateKey);
        translate.set("remark",remark);
        translate.set("update_time",new Date());
        log.info("更新：{}",JSON.toJSONString(translate));
        for (int i=0;i<kvlist.size();i++){
            KV kv=kvlist.get(i);
            translate.set(kv.getKey(),kv.getValue());
        }
        Db.update("sys_translate","translate_id", translate);
        Jboot.sendEvent(EventConstants.LOADTRANSLATEEVENT,"");
        return AjaxResult.success();
    }
    public AjaxResult delete(){
        Integer translateId=getInt("translateId");
        Db.deleteById("sys_translate","translate_id", translateId);
        Jboot.sendEvent(EventConstants.LOADTRANSLATEEVENT,"");
        return AjaxResult.success();
    }
}
